/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool dfs(TreeNode* root, TreeNode* subRoot) {
        if (root != nullptr && subRoot == nullptr)return 0;
        if (root == nullptr && subRoot != nullptr)return 0;
        if (root == nullptr && subRoot == nullptr)return 1;
        if (root->val != subRoot->val)return 0;
        return dfs(root->left, subRoot->left) && dfs(root->right, subRoot->right);
    }
    bool dfs1(TreeNode* root, TreeNode* subRoot) {
        if (!root)return 0;
        return dfs(root, subRoot) || dfs1(root->left, subRoot) || dfs1(root->right, subRoot);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        return dfs1(root, subRoot);
    }
};